package org.example.binaryoperator;

import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;

public class Java8BinaryOperator4 {
    public static void main(String[] args) {
        Developer dev1 = new Developer("jordan", BigDecimal.valueOf(9999));
        Developer dev2 = new Developer("jack", BigDecimal.valueOf(8888));
        Developer dev3 = new Developer("jaden", BigDecimal.valueOf(10000));
        Developer dev4 = new Developer("ali", BigDecimal.valueOf(2000));
        Developer dev5 = new Developer("mkyong", BigDecimal.valueOf(1));

        List<Developer> list = Arrays.asList(dev1, dev2, dev3, dev4, dev5);

        Comparator<Developer> comparator = Comparator.comparing(Developer::getSalary);
        BinaryOperator<Developer> bo = BinaryOperator.maxBy(comparator);
        Developer developer = find(list,bo);
        System.err.println(JSONObject.toJSONString(developer));

        Developer minDeveloper = find(list,BinaryOperator.minBy(Comparator.comparing(Developer::getSalary)));
        System.err.println(JSONObject.toJSONString(minDeveloper));

    }

    static Developer find(List<Developer> developers,BinaryOperator<Developer> bo){
        Developer result = null;
        for (Developer developer : developers) {
            if (result == null) {
                result = developer;
            }else{
                result = bo.apply(result,developer);
            }
        }
        return result;
    }

    static class Developer{
        private String name;
        private BigDecimal salary;

        public Developer(String name, BigDecimal salary) {
            this.name = name;
            this.salary = salary;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public BigDecimal getSalary() {
            return salary;
        }

        public void setSalary(BigDecimal salary) {
            this.salary = salary;
        }
    }
}
